<template>
    <div class="vue-uploader">
        <div class="file-list">
            <section v-for="(url, index) of list" class="file-item draggable-item">
                <img v-if="isImg(url)" :src="url" alt="" ondragstart="return false;">
				<video :src="url" v-else-if="isVideo(url)" controls="controls">
                </video>
				<div v-else class="none">
					格式错误
				</div>
                <span class="file-remove" @click="remove(index)">-</span>
            </section>
			<section v-for="item of uploadingList" class="file-item draggable-item">
				<div class="loading">
					<Spin></Spin>
				</div>
            </section>
		
            <section class="file-item" v-if="canAdd()">
                <div @click="add" class="add">
                    <span>+</span>
                </div>
            </section>
        </div>
        <input v-if="'1'==multiple" multiple type="file" v-bind:accept="accept" @change="fileChanged" ref="file">
		<input v-else type="file" v-bind:accept="accept" @change="fileChanged" ref="file">
    </div>
</template>
<script>
    export default {
		name:'uploader',
        props: {
            url: {
                type: String,
                required: true
            },
			list:{
				type: Array,
				required: true
			},
			max:{
				type: Number,
				default:5
			},
			accept:{
				type: String,
				default:'image/*'
			},
			multiple:{
				type: String,
				default:'0'
			}
        },
        data() {
            return {
				uploadingList:[]
            }
        },
		computed:{
			done:function(){
				return this.uploadingList.length == 0;
			}
		},
        methods: {
			canAdd(){
				return this.list.length < this.max && this.done;
			},
            add() {
                this.$refs.file.click()
            },
			isImg(item) {
                var index1=item.lastIndexOf(".");
				var index2=item.length;
				var suffix=item.substring(index1+1,index2);//后缀名
				var flag = (suffix == 'jpg')|(suffix == 'png')|(suffix == 'gif')|(suffix == 'jpeg') ;
				return flag;
            },
			isVideo(item) {
                var index1=item.lastIndexOf(".");
				var index2=item.length;
				var suffix=item.substring(index1+1,index2);//后缀名
				var flag = (suffix == 'mp4')|(suffix == 'avi')|(suffix == 'mpeg')|(suffix == 'wmv')|(suffix == 'mov')|(suffix == 'mkv')|(suffix == 'flv')|(suffix == 'f4v')|(suffix == 'm4v')|(suffix == 'rmvb')|(suffix == 'rm')|(suffix == '3gp')|(suffix == 'dat')|(suffix == 'ts')|(suffix == 'mts')|(suffix == 'vob');
				return flag;
            },
            remove(index) {
                this.list.splice(index, 1)
            },
            fileChanged() {
                const list = this.$refs.file.files
				if(this.list.length + list.length > this.max){
					if(this.$Message){
						this.$Message.info('最多只能同时上传'+this.max+'个文件');
					}else{
						alert('最多只能同时上传'+this.max+'个文件');
					}
					this.$refs.file.value = ''
				}else{
					for (let i = 0; i < list.length; i++) {
						const item = {
							name: list[i].name,
							size: list[i].size,
							file: list[i]
						}
						this.upload(list[i])
					}
					this.$refs.file.value = ''
				}                
            },
            // 将图片文件转成BASE64格式
            upload(file){
				var that = this;
				const formData = new FormData()
                formData.append('file', file)
                const xhr = new XMLHttpRequest();
                xhr.open('POST', this.url, true);
                xhr.send(formData);
				that.uploadingList.push(formData);
                xhr.onload = () => {
                    if (xhr.status === 200 || xhr.status === 304) {
						let response = xhr.responseText;
						try{
							var data = JSON.parse(response);
							if(data.code == 0){
								that.list.push(data.url);
							}
						}
						catch(err)
						{
						  console.log('upload error!');  
						}
                    } else {
                        console.log(`error：error code ${xhr.status}`)
                    }
					that.uploadingList.pop();
                }
            }
        }
    }
</script>
<style>
.vue-uploader {
    border: 1px solid #e5e5e5;
}
.vue-uploader .file-list {
    padding: 10px 0px;
}
.vue-uploader .file-list:after {
    content: '';
    display: block;
    clear: both;
    visibility: hidden;
    line-height: 0;
    height: 0;
    font-size: 0;
}
.vue-uploader .file-list .file-item {
    float: left;
    position: relative;
    width: 100px;
    text-align: center;
}
.vue-uploader .file-list .file-item img{
    width: 80px;
    height: 80px;
    border: 1px solid #ececec;
}
.vue-uploader .file-list .file-item video{
    width: 80px;
    height: 80px;
    border: 1px solid #ececec;
}
.vue-uploader .file-list .file-item .loading{
    width: 80px;
    height: 80px;
    border: 1px solid #ececec;
}
.vue-uploader .file-list .file-item .none{
    width: 80px;
    height: 80px;
    border: 1px solid #ececec;
}
.vue-uploader .file-list .file-item .file-remove {
    position: absolute;
    right: 12px;
    display: none;
    top: 4px;
    width: 14px;
    height: 14px;
    color: white;
    cursor: pointer;
    line-height: 12px;
    border-radius: 100%;
    transform: rotate(45deg);
    background: rgba(0, 0, 0, 0.5);
}
.vue-uploader .file-list .file-item:hover .file-remove {
    display: inline;
}
.vue-uploader .file-list .file-item .file-name {
    margin: 0;
    height: 40px;
    word-break: break-all;
    font-size: 14px;
    overflow: hidden;
    text-overflow: ellipsis;
    display: -webkit-box;
    -webkit-line-clamp: 2;
    -webkit-box-orient: vertical;
}
.vue-uploader .add {
    width: 80px;
    height: 80px;
    margin-left: 10px;
    float: left;
    text-align: center;
    line-height: 80px;
    border: 1px dashed #ececec;
    font-size: 30px;
    cursor: pointer;
}
.vue-uploader .upload-func {
    display: flex;
    padding: 10px;
    margin: 0px;
    background: #f8f8f8;
    border-top: 1px solid #ececec;
}
.vue-uploader .upload-func .progress-bar {
    flex-grow: 1;
}
.vue-uploader .upload-func .progress-bar section {
    margin-top: 5px;
    background: #00b4aa;
    border-radius: 3px;
    text-align: center; 
    color: #fff;
    font-size: 12px;
    transition: all .5s ease;
}
.vue-uploader .upload-func .operation-box {
    flex-grow: 0;
    padding-left: 10px;
}
.vue-uploader .upload-func .operation-box button {
    padding: 4px 12px;
    color: #fff;
    background: #007ACC;
    border: none;
    border-radius: 2px;
    cursor: pointer;
}
.vue-uploader > input[type="file"] {
    display: none;
}
</style>